# # -*- coding: utf-8 -*-


import os
from typing import Tuple

from PIL import Image
from rich.progress import Progress

from utils.ConsoleIO import bool_str, format_ipt, int_str, ipt, isin, prt

__version__ = "0.0.2"

pic_path = format_ipt(
    "请输入图片地址：",
    os.path.isfile,
    "文件错误或不存在：{}",
)[0]

picture = Image.open(pic_path)

size = format_ipt(
    "转换横长/块：",
    int_str,
    "值必须为整数：{}",
)[1]


pixel_per_block = format_ipt(
    "粒子密度/点每块：",
    int_str,
    "值必须为整数：{}",
)[1]

enable_depth: bool = format_ipt("启用深度/是否：", bool_str, "请输入逻辑值：{}")[1]

particle_name = ipt(
    "粒子效果名称：",
)
particle_name = particle_name if ":" in particle_name else "minecraft:" + particle_name

aspect = format_ipt(
    "横向|竖向：",
    isin,
    "请输入横/h|竖/v方位：{}",
    False,
    {
        True: ("横向", "横", "width", "wide", "horizontal", "h"),
        False: ("竖向", "竖", "height", "high", "vertical", "v"),
    },
)

dist_file = open(os.path.splitext(pic_path)[0] + "0.mcfunction", "w", encoding="utf-8")

block_per_pixel = 1 / pixel_per_block

if size == 0:
    size = picture.size
    picture_bw = picture.convert("L")
else:
    size: Tuple[int, int] = (size * pixel_per_block, round(size * pixel_per_block / picture.width * picture.height))  # type: ignore
    picture_bw = picture.convert("L").resize(
        size,
    )

total_count = 0


with Progress() as progress:
    total_task_start = progress.add_task(
        "[#0089F2 on #121110]处理图片像素……", total=size[0] * size[1]
    )

    for y in range(picture_bw.height):
        for x in range(picture_bw.width):
            depth = picture_bw.getpixel((x, y))
            if enable_depth:
                if depth != 255:
                    dist_file.write(
                        "particle {} ^{} ^{} ^{}\n".format(
                            particle_name,
                            *(
                                (
                                    block_per_pixel * (x - (size[0] / 2)),
                                    0,
                                    block_per_pixel * (y - (size[1] / 2)),
                                )
                                if aspect
                                else (
                                    block_per_pixel * ((size[0] / 2) - x),
                                    10 + block_per_pixel * (size[1] - y),
                                    block_per_pixel * depth - total_count // 9999,
                                )
                            ),
                        )
                    )
                    total_count += 1
            else:
                if depth < 128:
                    dist_file.write(
                        "particle {} ^{} ^{} ^{}\n".format(
                            particle_name,
                            *(
                                (
                                    block_per_pixel * (x - (size[0] / 2)),
                                    0,
                                    block_per_pixel * (y - (size[1] / 2)),
                                )
                                if aspect
                                else (
                                    block_per_pixel * ((size[0] / 2) - x),
                                    10 + block_per_pixel * (size[1] - y),
                                    -total_count // 9999,
                                )
                            ),
                        )
                    )
                    total_count += 1

            if total_count % 9999 == 0:
                dist_file.close()
                dist_file = open(
                    os.path.splitext(pic_path)[0]
                    + "{}.mcfunction".format(total_count // 9999),
                    "w",
                    encoding="utf-8",
                )

            progress.update(
                total_task_start,
                description=f"[#0089F2 on #121110]处理图片像素 {y*size[0]+x}/{size[0]*size[1]}",
                advance=1,
            ),  # type: ignore

prt("转换完成！共 {} 条指令！".format(total_count))

dist_file.close()
